<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="inc/default :: default"></head>
<body style="background-color: #fff">
<div class="layui-card ad-table-card" id="ad-table-card">
    <div class="layui-card-body">
        <table class="layui-hide" id="table1" lay-filter="form1"></table>
        <script type="text/html" id="barTools">
            <a class="ad-btn-link" lay-event="detail">查看</a>
        </script>
    </div>
</div>
<form class="layui-form">
    <button type="submit" class="layui-btn" lay-submit lay-filter="submit1" style="display:none;"></button>
</form>
</body>
<script type="text/javascript" th:inline="javascript">
    /*<![CDATA[*/
    var ctxPath = /*[[@{/}]]*/ '';
    /*]]>*/
    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引

    ;!function(){
        var layer1 = layui.layer
            ,table = layui.table
            ,form = layui.form;

        table =  $.extend(table, {config: {checkName: 'checked'}});
        table.render({
            id: 'idTable1'
            ,elem: '#table1'
            ,method: 'post'
            ,skin: 'line'
            ,cellMinWidth: 100
            ,limit: 100
            ,url: ctxPath + 'systemUser/roleList'
            ,where: {'userId': [[${systemUser.userId}]]}
            ,cols: [ [
                {type:'checkbox'}
                ,{field:'name', title: '角色名', align : 'center'}
                ,{field:'note', title: '备注', align : 'center'}
            ] ]
            ,response: {
                statusName: 'searchCount'
                ,statusCode: true
                ,countName: 'total'
                ,dataName: 'records'
            }
            ,page: false
        });

        form.on('submit(submit1)', function(data){
            var datas = table.checkStatus('idTable1').data;
            if (datas.length == 0) {
                layer1.msg("请选择角色！", {
                    icon : 2,
                    anim : 6,
                    time : 2000
                });
                return false;
            }
            var pks = [];
            $.each(datas, function (index, data) {
                pks.push(data.roleId);
            });
            $.ajax({
                type : 'post',
                url : ctxPath + 'systemUser/updateRole',
                data : {
                    'userId': [[${systemUser.userId}]],
                    'roleIds': pks.join(",")
                },
                success : function(result) {
                    if (result) {
                        layer1.msg("设置角色成功！", {
                            icon : 1,
                            time : 2000
                        }, function() {
                            if (index) {
                                parent.layer.close(index);
                            }
                        });
                    } else {
                        layer1.msg("设置角色失败！", {
                            icon : 2,
                            anim : 6,
                            time : 2000
                        });
                    }
                },
                error : function() {
                    layer1.msg("设置角色失败！", {
                        icon : 2,
                        anim : 6,
                        time : 2000
                    });
                }
            });
            return false;
        });
    }();
</script>